<?php
/*
 * 员工工资接口
 */
class Apiv1_WagesController extends Ctrl_Api{
    public function indexAction(){
        $p = $_REQUEST;
        $pDepartment = empty($p['department'])?'':$p['department'];
        $pWid = empty($p['id'])?0:intval($p['id']);
        $pOpt = empty($p['opt'])?'':trim($p['opt']);
        $tOptarr = array('up','down');
        $pSize = empty($p['size'])?5:intval(trim($p['size']));

        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;
        $tLMO = new LeavedModel;
        $tWgMO = new WagesModel;

        $tTime = time();

		if(!in_array($pOpt,$tOptarr)){Tool_Fnc::appMsg('操作类型错误');}
        $tWhere = ' 1 ';

        if(!empty($pDepartment)){
            $tWhere .= ' and w.department = '.$pDepartment;     
        }

        if($pOpt == 'up'){
            $tWhere .= ' and w.id < '.$pWid;
        }else{
            $tWhere .= ' and w.id > '.$pWid;
        }

        $tSql = 'select count(w.id) c from '.$tWMO->table.' w left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere;
        $tCount = $tWMO->query($tSql);
        $tCnt = empty($tCount[0]['c'])?0:$tCount[0]['c'];

        $tTime = time();

        $tDatas = array();
        if(!empty($tCnt)){
            $tSql = 'select w.id id,w.name name,w.hiredate hiredate,w.probation_period probation_period,w.probation_fee probation_fee,w.fee fee,di.introduce department from '.$tWMO->table.' w left join '.$tDIMO->table.' di on w.department = di.num where '.$tWhere.' order by w.id desc limit 0,'.$pSize;
            $tDatas = $tWMO->query($tSql);
            foreach($tDatas as $tK=>$tV){
                $tSDays = array();
                //判断是不是试用期 
                $tSDays = $this->timeDiff(strtotime($tV['hiredate']),$tTime);
                if($tSDays['day'] > intval($tV['probation_period'])){#不是试用期，基本工资按转正工资
                    $tDatas[$tK]['basic_fee'] = $tV['fee'];
                }else{
                    $tDatas[$tK]['basic_fee'] = $tV['probation_fee'];
                }
                //获取请假天数
                $tLData = $tLMO->field('sum(leavedays) leavedays')->where('w_id = '.$tV['id'])->fList();
                $tLData = empty($tLData[0]['leavedays'])?0:$tLData[0]['leavedays'];
                //计算出勤天数
                //判断是否是新入职员工
                $tLDays = date('t',strtotime(date('Y-m-d',$tTime).' -1 month'));
                $tDatas[$tK]['attendence_days'] = $tLDays - $tLData;
                if(date('Y-m',strtotime($tV['hiredate'].' +1 month')) == date('Y-m',$tTime)){
                    $tFirstday = date('Y-m-01',strtotime($tV['hiredate']));
                    $tDays = $this->timeDiff(strtotime($tV['hiredate']),strtotime($tFirstday.' +1 month -1 day'));
                    $tDatas[$tK]['attendence_days'] = $tDays['day'] - $tLData;
                }
                //计算出应发工资
                $tDatas[$tK]['prefee'] = $tDatas[$tK]['basic_fee'] * $tLDays; 
                //获取实发工资和发放状态
                $tWgData = $tWgMO->field('payment,is_issue')->where('w_id = '.$tV['id'])->fRow();
                $tStatus = $tWgMO->field('count(id) c')->where('w_id = '.$tV['id'].' and FROM_UNIXTIME(created,"%Y-%m") = "'.date('Y-m',$tTime).'"')->fList();
                $tDatas[$tK]['payment'] = empty($tWgData['payment'])?0:$tWgData['payment'];
                $tDatas[$tK]['is_issue'] = empty($tStatus[0]['c'])?0:1;
                
            }
        }
    
        Tool_Fnc::ajaxMsg('',1,$tDatas);
    }
    public function editAction(){
        $p = $_REQUEST;

        $tWMO = new WorkerModel;
        $tDIMO = new DepartmentinfoModel;
        $tLMO = new LeavedModel;
        $tWgMO = new WagesModel;

        $tTime = time();

            $pPayment = empty($p['payment'])?Tool_Fnc::ajaxMsg('实发工资不能为空'):Tool_Fnc::safe_string($p['payment']); 
            $pId = empty($p['id'])?Tool_Fnc::ajaxMsg('员工ID不能为空'):Tool_Fnc::safe_string($p['id']);
            $pBasicfee = empty($p['basic_fee'])?Tool_Fnc::ajaxMsg('基本工资不能为空'):Tool_Fnc::safe_string($p['basic_fee']);
            $pAttendencedays = empty($p['attendence_days'])?Tool_Fnc::ajaxMsg('实出勤天数不能为空'):Tool_Fnc::safe_string($p['attendence_days']);
            $pPrefee = empty($p['prefee'])?Tool_Fnc::ajaxMsg('应发工资不能为空'):Tool_Fnc::safe_string($p['prefee']);

            $tData = array(
                'w_id' => $pId,
                'basic_fee' => $pBasicfee,
                'attendence_days' => $pAttendencedays,
                'prefee' => $pPrefee,
                'payment' => $pPayment,
                'is_issue' => 1,
                'handlers' => $_SESSION['admin']['name'],
                'created' => $tTime,
            );
            if(!$tWgMO->insert($tData)){
                Tool_Fnc::ajaxMsg('操作失败');
            }
            Tool_Fnc::ajaxMsg('操作成功',1);
    }
    //时间差函数
    public function timeDiff( $begin_time, $end_time ){
        if ( $begin_time < $end_time ) {
            $starttime = $begin_time;
            $endtime = $end_time;
        } else {
            $starttime = $end_time;
            $endtime = $begin_time;
        }
        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        $res = array( "day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs );
        return $res;
    }
}
?>
